//
//  MYGMedicalToolsBMITagView.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/10.
//

import UIKit

enum MYGMedicalToolsBMILevel {
    case thin
    case normal
    case over
    case heavy
    case superHeavy
    
    var title: String {
        switch self {
        case .thin:
            "偏瘦"
        case .normal:
            "正常"
        case .over:
            "超重"
        case .heavy:
            "偏胖"
        case .superHeavy:
            "肥胖"
        }
    }
    
    var themeColor: UIColor {
        switch self {
        case .thin:
            UIColor.rgba(255, 222, 106)
        case .normal:
            UIColor.rgba(148, 205, 27)
        case .over:
            UIColor.rgba(235, 129, 21)
        case .heavy:
            UIColor.rgba(232, 90, 20)
        case .superHeavy:
            UIColor.rgba(201, 57, 59)
        }
    }
    
    var ratio: CGFloat {
        switch self {
        case .thin:
            0.185
        case .normal:
            0.21
        case .over:
            0.21
        case .heavy:
            0.21
        case .superHeavy:
            0.185
        }
    }
    
    
    static func getTotalRadio(by bmi: CGFloat) -> CGFloat {
        let level = getLevel(by: bmi)
        if level == .thin {
            return (bmi / 18.5) * level.ratio
        } else if level == .normal {
            return MYGMedicalToolsBMILevel.thin.ratio + (bmi - 18.5) / (24 - 18.5) * level.ratio
        } else if level == .over {
            return MYGMedicalToolsBMILevel.thin.ratio + MYGMedicalToolsBMILevel.normal.ratio + MYGMedicalToolsBMILevel.over.ratio / 2
        } else if level == .heavy {
            return MYGMedicalToolsBMILevel.thin.ratio + MYGMedicalToolsBMILevel.normal.ratio + MYGMedicalToolsBMILevel.over.ratio  + (bmi - 24) / (27.9 - 24) * level.ratio
        } else {
            return MYGMedicalToolsBMILevel.thin.ratio + MYGMedicalToolsBMILevel.normal.ratio + MYGMedicalToolsBMILevel.over.ratio + MYGMedicalToolsBMILevel.heavy.ratio + (bmi - 27.9) / (27.9 - 24) * level.ratio
        }
    }
        
    static func getLevel(by bmi: CGFloat) -> MYGMedicalToolsBMILevel {
        if bmi < 18.5 {
            return .thin
        } else if bmi < 24 {
            return .normal
        } else if bmi < 27.9 {
            return .heavy
        } else {
            return .superHeavy
        }
    }
}


class MYGMedicalToolsBMITagView: UIView {
    // MARK: - Property
    private var containerWidth: CGFloat
    private var levelViewMap: [MYGMedicalToolsBMILevel: UIView] = [:]
    // MARK: - init
    init(containerWidth: CGFloat = screenWidth, frame: CGRect) {
        self.containerWidth = containerWidth
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        self.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.right.top.bottom.equalToSuperview()
            make.height.equalTo(54)
            make.left.right.equalToSuperview()
        }
        
        contentView.addSubview(tagImageView)
        tagImageView.addSubview(tagLabel)
        tagLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(14)
        }
    
        let levels: [MYGMedicalToolsBMILevel] = [.thin, .normal, .over, .heavy, .superHeavy]
        var lastView: UIView?
        for level in levels {
            let view = UIView()
            view.bounds = CGRect(x: 0, y: 0, width: level.ratio * containerWidth, height: 8)
            view.backgroundColor = level.themeColor
            if level == .thin {
                view.roundCorners(UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.bottomLeft.rawValue), radius: 4)
            } else if level == .superHeavy {
                view.roundCorners(UIRectCorner(rawValue: UIRectCorner.topRight.rawValue | UIRectCorner.bottomRight.rawValue), radius: 4)
            }
            contentView.addSubview(view)
            let width = containerWidth - 4
            view.snp.makeConstraints { make in
                make.width.equalTo(level.ratio * width)
                if (lastView == nil) {
                    make.left.equalToSuperview()
                } else {
                    make.left.equalTo(lastView!.snp.right).offset(1)
                }
                make.top.equalTo(22.5)
                make.height.equalTo(8)
            }
            
            let label = UILabel()
            label.font = .regular(10)
            label.textColor = .rgba(172, 172, 172)
            label.text = level.title
            contentView.addSubview(label)
            label.snp.makeConstraints { make in
                make.centerX.equalTo(view)
                make.top.equalTo(view.snp.bottom).offset(9.5)
            }
            
            lastView = view
            levelViewMap[level] = view
        }
    }
    
    // MARK: - Public funcs
    func update(bmi: CGFloat) {
        tagImageView.isHidden = false
        let level = MYGMedicalToolsBMILevel.getLevel(by: bmi)
        var ratio = MYGMedicalToolsBMILevel.getTotalRadio(by: bmi)
        if ratio > 1 {
            ratio = 1
        }
        tagImageView.snp.remakeConstraints { make in
            make.top.equalToSuperview()
            make.size.equalTo(CGSizeMake(34, 20.5))
            make.left.equalToSuperview().offset(containerWidth * ratio - 17)
        }
        
        tagImageView.tintColor = level.themeColor
        tagLabel.text = String(format: "%.1f", bmi)
    }
    
    // MARK: - lazy load
    private lazy var tagImageView: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "MYG_tools_tag")?.withRenderingMode(.alwaysTemplate))
        imageV.isHidden = true
        return imageV
    }()
    
    private lazy var tagLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(10)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    private lazy var contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        return view
    }()
    
}
